﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraRichEdit;
using System.IO;
using Qiniu.Storage;
using DICOMViewer.UC;
using System.Threading;
using Qiniu.Http;
using DICOMViewer.Model;

namespace DICOMViewer
{
    public partial class ReportForm : Form
    {
        public ReportForm()
        {
            InitializeComponent();
            this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
            this.WindowState = FormWindowState.Maximized;
            //this.ribbonControl1.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonControlStyle.
        }

        private string orderuuid;

        public ReportForm(string orderUuid) : this()
        {
            orderuuid = orderUuid;
            localFilePath = Application.StartupPath + string.Format("\\temp\\{0}.rtf", orderuuid);
        }

        private void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new System.Threading.Thread(() =>
            {
                this.Invoke(new EventHandler(delegate
                {
                    progressForm = new ProgressForm(this);
                    progressForm.counterLabel.Text = "开始上传文档！";
                    progressForm.ShowDialog();
                }));
            }).Start();
            new System.Threading.Thread(() =>
            {
                this.richEditControl1.SaveDocument(localFilePath, DocumentFormat.Rtf);
                FormUploaderEx uploader = new FormUploaderEx();
                uploader.OnUploadProgress += Uploader_OnUploadProgress;
                if (uploader.TryGetUploadConfig(LoginInfo.Current.Qiniuconfig))
                {
                    uploader.UploadFile(localFilePath, string.Format("{0}.rtf", orderuuid), null, null);
                    this.Invoke(new EventHandler(delegate
                    {
                        DialogResult dr = MessageBox.Show("保存完毕！是否关闭本页面？", "提示", MessageBoxButtons.YesNo);
                        if (dr == DialogResult.Yes)
                        {
                            File.Delete(localFilePath);
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                    }
                    ));
                }
                else
                {
                    MessageBox.Show("在线获取上传设置失败！请检查网络后重试！");
                }
            }).Start();
        }

        private ProgressForm progressForm;
        private string localFilePath="D:\\1.rtf";

        private void Uploader_OnUploadProgress(long uploadedBytes, long totalBytes)
        {
            this.Invoke(new EventHandler(delegate
            {
                double percent = 100.0*uploadedBytes/totalBytes;
                if (uploadedBytes < totalBytes)
                {
                    progressForm.counterLabel.Text = string.Format("正在上传文档... ({0,7:0.000}%)", percent);
                    progressForm.progressCounter.Value = (int) percent;
                }
                else
                {
                    progressForm.DialogResult = DialogResult.OK;
                    progressForm.Close();
//                    progressForm.Dispose();
//                    progressForm = null;
                }
            }));
        }

        private void bbiClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void TestForm_Shown(object sender, EventArgs e)
        {
            WaitForm form = new WaitForm(this);
            Thread th = new Thread(() =>
            {
                DownloadManagerEx download = new DownloadManagerEx();
                string downUrl = download.CreatePublishUrl(orderuuid + ".rtf");
                //运用时间戳获取文件最新版本！
                TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                HttpResult hr = download.Download(string.Format("http://{0}?{1}",downUrl, ts.TotalSeconds), localFilePath);
                this.Invoke(new EventHandler(delegate
                {
                    if (hr.Code == 404)
                    {
                        //网络上没有对应文件
                    }
                    else if (hr.Code == 200)
                    {
                        //获取到了最新的文件！


                        this.richEditControl1.LoadDocument(localFilePath);
                    }
                    else
                    {
                        MessageBox.Show(string.Format("发生网络通讯错误，请重试！错误内容：{0}", hr.ToString()));
                    }
                    form.DialogResult = DialogResult.OK;
                }));
            });
            th.Start();

            form.ShowDialog();
        }
    }
}